<?php
/**
 * Simple logger class based on a similer class created by
 * Darko Bunic (http://www.redips.net/php/write-to-log-file/)
 * Does simple logging to a specified file. See https://bitbucket.org/huntlyc/simple-php-logger for more details.
 *
 * @package default
 * @author Huntly Cameron <huntly.cameron@gmail.com>
 **/

namespace App\Common;


define('LEVEL_FATAL', 0);
define('LEVEL_ERROR', 1);
define('LEVEL_WARN', 2);
define('LEVEL_INFO', 3);
define('LEVEL_DEBUG', 4);

class Logger
{
    /**
     * log_file - the log file to write to
     *
     * @var string
     **/
    private $log_file;

    private $level = LEVEL_DEBUG;

    /**
     * Constructor
     * @param String logfile - [optional] Absolute file name/path. Defaults to ubuntu apache log.
     * @return void
     **/
    function __construct()
    {

        //日志文件按照当天的文件夹来存储
        $today = date('Y-m-d');
        if (!is_dir(storage_path('logs/'.$today))) {
            mkdir(storage_path('logs/'.$today));
        }
        $this->log_file = storage_path('logs/'.$today.'/'.date('H点').'web.log');

        if (!file_exists($this->log_file)) { //Attempt to create log file
            touch($this->log_file);
        }
    }

    public static function instance()
    {

        return new self();
    }

    public function setLevel($status)
    {
        $this->level = $status;
    }

    /**
     * debug - Log Debug
     * @param String tag - Log Tag
     * @param String message - message to spit out
     * @return void
     **/
    public function debug($tag, $message)
    {
        $this->writeToLog("DEBUG", LEVEL_DEBUG, $tag, $message);
    }

    /**
     * info - Log Info
     * @param String tag - Log Tag
     * @param String message - message to spit out
     * @return void
     **/
    public function info($tag, $message)
    {
        $this->writeToLog("INFO", LEVEL_INFO, $tag, $message);
    }

    /**
     * warn - Log Warning
     * @param String tag - Log Tag
     * @param String message - message to spit out
     * @author
     **/
    public function warn($tag, $message)
    {
        $this->writeToLog("WARN", LEVEL_WARN, $tag, $message);
    }

    /**
     * error - Log Error
     * @param String tag - Log Tag
     * @param String message - message to spit out
     * @author
     **/
    public function error($tag, $message)
    {
        $this->writeToLog("ERROR", LEVEL_ERROR, $tag, $message);
    }

    /**
     * fatal - Log Fatal
     * @param String tag - Log Tag
     * @param String message - message to spit out
     * @author
     **/
    public function fatal($tag, $message)
    {
        $this->writeToLog("FATAL", LEVEL_FATAL, $tag, $message);
    }

    /**
     * writeToLog - writes out timestamped message to the log file as
     * defined by the $log_file class variable.
     *
     * @param String status - "INFO"/"DEBUG"/"ERROR" e.t.c.
     * @param String tag - "Small tag to help find log entries"
     * @param String message - The message you want to output.
     * @return void
     **/
    private function writeToLog($status, $level, $tag, $message)
    {
        if ($level <= $this->level) {
            $date = date('[Y-m-d H:i:s]');
            $msg  = "$date: [$tag][$status][process_id:".getmypid().']'. "- $message." . PHP_EOL;
            file_put_contents($this->log_file, $msg, FILE_APPEND);
        }
    }
}